import { defineStore } from 'pinia'
import { login } from '@/api/login'
import { local } from '@/utils/storage'

export const useUserStore = defineStore('user', {
  persist: {
    enabled: true,
    strategies: [
      {
        key: 'XZ-admin-user',
        storage: localStorage,
      },
    ],
  },
  state() {
    return {
      user: {},
    }
  },
  actions: {
    setUser(user) {
      this.user = user
    },
    login(data) {
      const { user, token } = login(data)
      console.log(' user, token: ', user, token)

      this.setUser(user)
      local.set('admin-token', token)
    },
    logout() {
      local.remove('admin-user')
      local.remove('admin-token')
    },
  },
})
